package com.difed.cccam.api;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;

import com.difed.cccam.api.util.StringUtils;

import android.util.Log;

public class ConexionImpl implements Conexion {
    
    private static final String HOME = "/";
    private static final String CLIENTES = "/clients";
    private static final String SHARED = "/shares";
    private static final String SERVIDORES = "/servers";
    
    private String host;
    private String usuario;
    private String password;

	public ConexionImpl(String host, String usuario, String password) {
        super();
        this.host = host;
        this.usuario = usuario;
        this.password = password;
    }

    @Override
    public String home() {
        return conectar(GET_TYPE, host + HOME, usuario, password);
    }

    @Override
	public String clientes() {
        return conectar(GET_TYPE, host + CLIENTES, usuario, password);
	}

	@Override
	public String compartido() {
	    return conectar(GET_TYPE, host + SHARED, usuario, password);
	}

	@Override
	public String servidor() {
	    return conectar(GET_TYPE, host + SERVIDORES, usuario, password);
	}
	
	public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    protected String conectar(int requestType, String URL, String user, String pass) {
        DefaultHttpClient client = new DefaultHttpClient();
        if ((user != null) && (pass != null)) {
            client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, pass));
        }
        HttpRequestBase method = null;
        if (requestType == POST_TYPE) {
            method = new HttpPost(URL);
        } else if (requestType == GET_TYPE) {
            method = new HttpGet(URL);
        }
        
        String result = null;
        if (method != null) {
            try {
                HttpResponse response = client.execute(method);
                StatusLine status = response.getStatusLine();
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    try {
                        result = StringUtils.inputStreamToString(entity.getContent());
                    } catch (IOException e) {
                        Log.e("CCCAM", e.getMessage());
                    }
                } else {
                    Log.e("CCCAM", status.getReasonPhrase());
                }
            } catch (IOException e) {
                Log.e("CCCAM", e.getMessage());
            }
        }
        return result;
    }
}
